#Replication Code File for "Does Social Media Promote Civic Activism? A Field Experiment with a Civic Campaign"

rm(list=ls())

install.packages("stargazer", dependencies=TRUE)
install.packages("effsize", dependencies=TRUE)
install.packages("aod", dependencies=TRUE)
install.packages("car", dependencies = TRUE)
install.packages("tidyverse", dependencies=TRUE)
install.packages("sm", dependencies=TRUE)
install.packages("AER", dependencies=TRUE)
install.packages("ri")
install.packages("foreign")
install.packages("readstata13")
install.packages("randomizr")
install.packages("estimatr")
install.packages("ggplot2")
install.packages("plm")
install.packages("xtable")

sI <- sessionInfo()
sI

library(stargazer)
library(effsize)
library(aod)
library(car)
library(tidyverse)
library(sm)
library(AER)
library(ri)
library(foreign)
library(readstata13)
library(randomizr)
library(estimatr)
library(ggplot2)
library(plm)
library(xtable)

set.seed(1234567) 

#setwd("") # set working directory


#Table 1

Eurobarometer<-read.dta13("Eurobarometer.dta")

names(Eurobarometer)

tab1<-
  Eurobarometer %>% 
  group_by(Country)  %>% 
  summarise(mean_voice = mean(voice, na.rm=TRUE),
            mean_say = mean(say, na.rm=TRUE))

table1<-xtable(tab1, digits=2)


#Part 4

data <- read.dta13("data_fb.dta")

subsample_var<-subset(data, select=c(assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving, fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, edu_1, edu_2, edu_3, edu_4, edu_5, status_1, status_2, status_3, status_4, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis, frequency_fb, frequency_fb_mis, news_2, news_3, news_4, civic_re, civic_mis, environment_act_2, environment_act_3, vote_re, vote_mis, scale_trust, role_2, role_3, role_4, knowledgescale, nature_re, nature_mis, danger, danger_mis, adventure, adventure_mis, pleasure, pleasure_mis, ability, ability_mis))

#Recode

subsample_var$uni[subsample_var$edu_1==1] <- 1
subsample_var$uni[subsample_var$edu_2==1] <- 0
subsample_var$uni[subsample_var$edu_3==1] <- 0
subsample_var$uni[subsample_var$edu_4==1] <- 0
subsample_var$uni[subsample_var$edu_5==1] <- 0

subsample_var$status_per[subsample_var$status_1==1] <- 1
subsample_var$status_per[subsample_var$status_2==1] <- 1
subsample_var$status_per[subsample_var$status_3==1] <- 0
subsample_var$status_per[subsample_var$status_4==1] <- 0
subsample_var$status_per[subsample_var$status_5==1] <- 0


subsample_var<-subset(subsample_var, select=c(assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving, fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, uni, edu_5, status_per, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis,  frequency_fb, frequency_fb_mis, news_2, news_3, news_4, civic_re, civic_mis, environment_act_2, environment_act_3, vote_re, vote_mis, scale_trust, role_2, role_3, role_4, knowledgescale, nature_re, nature_mis, danger, danger_mis, adventure, adventure_mis, pleasure, pleasure_mis, ability, ability_mis))

subsample_var$blocks[subsample_var$stream=="representative"] <-0
subsample_var$blocks[subsample_var$stream=="activists"] <-1


#Analysis for Table 2

subsample_var_all<-subset(subsample_var, select=c(blocks, assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving, fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, uni, edu_5, status_per, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis, frequency_fb, frequency_fb_mis, news_2, news_3, news_4, role_2, role_3, role_4, scale_trust, knowledgescale, nature_re, nature_mis, danger, danger_mis, adventure, adventure_mis, pleasure, pleasure_mis, ability, ability_mis, civic_re, civic_mis, environment_act_2, environment_act_3,  vote_re, vote_mis))

subsample_var_demoplus<-subset(subsample_var, select=c(blocks, assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving,  fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, uni, edu_5, status_per, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis,  frequency_fb, frequency_fb_mis, news_2, news_3, news_4, role_2, role_3, role_4, scale_trust, knowledgescale, nature_re, nature_mis, danger, danger_mis, adventure, adventure_mis, pleasure, pleasure_mis, ability, ability_mis))

subsample_var_demo<-subset(subsample_var, select=c(blocks, assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving, fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, uni, edu_5, status_per, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis,  frequency_fb, frequency_fb_mis))


Y <- subsample_var_demo$blocks # define Outcome
X<- as.matrix(subsample_var_demo[,17:31])
table(Y)
table(X)

lm_fit1 <- lm(Y~X, data=subsample_var_demo)

m1 <- coeftest(lm_fit1,vcovHC(lm_fit1, type="HC2"))  



Y <- subsample_var_demoplus$blocks # define Outcome
X<- as.matrix(subsample_var_demoplus[,17:49])

lm_fit2 <- lm(Y~X, data=subsample_var_demoplus)

m2 <- coeftest(lm_fit2,vcovHC(lm_fit2, type="HC2"))  


Y <- subsample_var_all$blocks # define Outcome
X<- as.matrix(subsample_var_all[,17:55])

lm_fit3 <- lm(Y~X, data=subsample_var_all)

m3 <- coeftest(lm_fit3,vcovHC(lm_fit3, type="HC2")) 


#Table 2

table2<-stargazer(m1, m2, m3,
          type = "latex", title ="", style = "AJPS",
          summary = NULL, out = NULL, out.header = FALSE,
          column.labels = NULL, column.separate = NULL,
          covariate.labels = NULL, dep.var.caption = NULL,
          dep.var.labels = NULL, dep.var.labels.include = TRUE,
          align = FALSE,
          coef = NULL, se = NULL, t = NULL, p = NULL,
          t.auto = TRUE, p.auto = TRUE,
          ci = FALSE, ci.custom = NULL,
          ci.level = 0.95, ci.separator = NULL,
          add.lines = NULL,
          apply.coef = NULL, apply.se = NULL,
          apply.t = NULL, apply.p = NULL, apply.ci = NULL,
          colnames = NULL,
          column.sep.width = "5pt",
          decimal.mark = NULL, df = TRUE,
          digit.separate = NULL, digit.separator = NULL,
          digits = NULL, digits.extra = NULL, flip = FALSE,
          float = TRUE, float.env="table",
          font.size = NULL, header = TRUE,
          initial.zero = NULL,
          intercept.bottom = FALSE, intercept.top = FALSE,
          keep = NULL, keep.stat = NULL,
          label = "", model.names = NULL,
          model.numbers = NULL, multicolumn = TRUE,
          no.space = NULL,
          notes = NULL, notes.align = NULL,
          notes.append = TRUE, notes.label = NULL,
          object.names = FALSE,
          omit = c("_mis", "Xregion", "Constant", "Xsex_3", "Xedu_5", "status_5", "Xrole_4", "frequency_fb_3", "environment_act_3", "Xnews_4"), omit.labels = NULL,
          omit.stat = NULL, omit.summary.stat = NULL,
          omit.table.layout = NULL,
          omit.yes.no = c("Yes", "No"),
          order = NULL, ord.intercepts = FALSE,
          perl = FALSE, report = NULL, rownames = NULL,
          rq.se = "nid", selection.equation = FALSE,
          single.row = TRUE,
          star.char = NULL, star.cutoffs = NULL,
          suppress.errors = FALSE,
          table.layout = NULL, table.placement = "!",
          zero.component = FALSE,
          summary.logical = TRUE, summary.stat = NULL,
          nobs = TRUE, mean.sd = TRUE, min.max = TRUE,
          median = FALSE, iqr = FALSE )



#Prepare data for Figure 1

subsample_var$blocks[subsample_var$stream=="representative"] <-0
subsample_var$blocks[subsample_var$stream=="activists"] <-1

subsample_var$representative[subsample_var$stream=="representative"] <-1
subsample_var$activist[subsample_var$stream=="activists"] <-1

subsample_var$fbcomply[subsample_var$assignment==2 & subsample_var$compliance==1] <- 1
subsample_var$fbcomply[subsample_var$assignment==2 & subsample_var$compliance==0] <- 0

subsample_var$newslettercomply[subsample_var$assignment==3 & subsample_var$compliance==1] <- 1
subsample_var$newslettercomply[subsample_var$assignment==3 & subsample_var$compliance==0] <- 0

subsample_var$comply_2cat[subsample_var$assignment==2 & subsample_var$fbcomply==1] <- 1
subsample_var$comply_2cat[subsample_var$assignment==3 & subsample_var$newslettercomply==1] <- 0


#Subset by variable

subsample_graph_civic<-subset(subsample_var,!is.na(civic))

subsample_graph_trust<-subset(subsample_var,!is.na(scale_trust))

subsample_graph_nature<-subset(subsample_var,!is.na(natureloving))

subsample_graph_danger<-subset(subsample_var,!is.na(danger_graph))


#Subset by sample type

block_civic <- factor(subsample_graph_civic$stream)

block_trust<- factor(subsample_graph_trust$stream)

block_nature<- factor(subsample_graph_nature$stream)

block_danger <- factor(subsample_graph_danger$stream)


#Figure 1

pdf(paste("civic_scale1.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_civic$civic, block_civic , xlim=c(0, 5), xlab="number of activities engaged in during past 12 months", col=c("black", "red"))
title(main="Distribution of civic activism scale")
legend("topright", levels(block_civic ), fill=1+(0:nlevels(block_civic )))
dev.off()


pdf(paste("trust_scale1.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_trust$scale_trust, block_trust, xlim=c(-3.5, 3.5), xlab="Trust in institutions", col=c("black", "red"))
title(main="Distribution of trust variable")
legend("topright", levels(block_trust), fill=1+(0:nlevels(block_trust)))
dev.off()


pdf(paste("nature_scale1.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_nature$natureloving, block_nature, xlim=c(1, 6), xlab="loves nature: 1=unlike me, 6=like me", col=c("black", "red"))
title(main="Distribution of love for nature")
legend("topright", levels(block_nature), fill=1+(0:nlevels(block_nature)))
dev.off()

pdf(paste("danger_scale1.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_danger$danger_graph, block_danger, xlim=c(1, 6), xlab="likes danger: 1=unlike me, 6=like me", col=c("black", "red"))
title(main="Distribution of danger-loving personality")
legend("topright", levels(block_danger), fill=1+(0:nlevels(block_danger)))
dev.off()



#Figure 2

subsample <- read.dta13("data_fb.dta")

subsample_var<-subset(subsample, select=c(assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving, fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, edu_1, edu_2, edu_3, edu_4, edu_5, status_1, status_2, status_3, status_4, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis, environment_act_2, environment_act_3, news_2, news_3, news_4, civic_re, civic_mis, frequency_fb, frequency_fb_mis, vote_re, vote_mis, scale_trust, role_2, role_3, role_4, knowledgescale, nature_re, nature_mis, danger, danger_mis, adventure, adventure_mis, pleasure, pleasure_mis, ability, ability_mis))

#Recode

subsample_var$uni[subsample_var$edu_1==1] <- 1
subsample_var$uni[subsample_var$edu_2==1] <- 0
subsample_var$uni[subsample_var$edu_3==1] <- 0
subsample_var$uni[subsample_var$edu_4==1] <- 0
subsample_var$uni[subsample_var$edu_5==1] <- 0


subsample_var$status_per[subsample_var$status_1==1] <- 1
subsample_var$status_per[subsample_var$status_2==1] <- 1
subsample_var$status_per[subsample_var$status_3==1] <- 0
subsample_var$status_per[subsample_var$status_4==1] <- 0
subsample_var$status_per[subsample_var$status_5==1] <- 0


#Subset sample

subsample_var<-subset(subsample_var, select=c(assignment, fblike, newsletter, role_graph, vote_graph, age, compliance, civic, danger_graph, adventure_graph, scale_trust, natureloving,  fbtreat, fbvsnewsletter, stream,  townpoplog, age_re, age_mis, sex_2, sex_3, uni, edu_5, status_per, status_5, friends_re, friends_mis, fbfriends_re, fbfriends_mis, frequency_fb, frequency_fb_mis, news_2, news_3, news_4, civic_re, civic_mis, environment_act_2, environment_act_3, vote_re, vote_mis, scale_trust, role_2, role_3, role_4, knowledgescale, nature_re, nature_mis, danger, danger_mis, adventure, adventure_mis, pleasure, pleasure_mis, ability, ability_mis))

subsample_var$blocks[subsample_var$stream=="representative"] <-0
subsample_var$blocks[subsample_var$stream=="activists"] <-1

subsample_var$representative[subsample_var$stream=="representative"] <-1
subsample_var$activist[subsample_var$stream=="activists"] <-1

subsample_var$comply4cat_1[subsample_var$fblike==1] <- 4
subsample_var$comply4cat_1[subsample_var$newsletter==1] <- 2
subsample_var$comply4cat_1[subsample_var$assignment==2 & subsample_var$fblike==0] <- 3
subsample_var$comply4cat_1[subsample_var$assignment==3 & subsample_var$newsletter==0] <-1

subsample_var$comply4cat[subsample_var$fblike==1] <- "1. fb like"
subsample_var$comply4cat[subsample_var$newsletter==1] <- "3. newsletter sign-up"
subsample_var$comply4cat[subsample_var$assignment==2 & subsample_var$fblike==0] <- "2. fb non-compliers"
subsample_var$comply4cat[subsample_var$assignment==3 & subsample_var$newsletter==0] <- "4. newsletter non-complier"

subsample_var_rep<-subset(subsample_var, !is.na(representative))

subsample_var_rep<-subset(subsample_var_rep, !is.na(comply4cat))


#subset for figure

subsample_graph_civic<-subset(subsample_var_rep,!is.na(civic))

subsample_graph_trust<-subset(subsample_var_rep,!is.na(scale_trust))

subsample_graph_nature<-subset(subsample_var_rep,!is.na(natureloving))

subsample_graph_danger<-subset(subsample_var_rep,!is.na(danger_graph))


comply_civic <- factor(subsample_graph_civic$comply4cat)

comply_trust<- factor(subsample_graph_trust$comply4cat)

comply_nature<- factor(subsample_graph_nature$comply4cat)

comply_danger <- factor(subsample_graph_danger$comply4cat)


#Figure 2

pdf(paste("civic_scale_comp.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_civic$civic, comply_civic, xlim=c(0, 5), xlab="civic activism scale, sum of activities in past 12 months", col=c("blue", "dodgerblue1", "black", "grey50"), lty=c(1, 2, 1, 2))
title(main="Distribution of civic activism scale")
legend("topright", levels(comply_civic), fill=c("blue", "dodgerblue1", "black", "grey50"))
dev.off()

pdf(paste("trust_scale_comp.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_trust$scale_trust, comply_trust, xlim=c(-3.5, 3.5), xlab="Trust in institutions, -3=no trust, 3=a lot of trust",  col=c("blue", "dodgerblue1", "black", "grey50"), lty=c(1, 2, 1, 2))
title(main="Distribution of trust in institutions")
legend("topright", levels(comply_nature), fill=c("blue", "dodgerblue1", "black", "grey50"))
dev.off()

pdf(paste("nature_scale_comp.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_nature$natureloving, comply_nature, xlim=c(1, 6), xlab="loves nature: 1=unlike me, 6=like me", col=c("blue", "dodgerblue1", "black", "grey50"), lty=c(1, 2, 1, 2))
title(main="Distribution of love for nature")
legend("topleft", levels(comply_nature), fill=c("blue", "dodgerblue1", "black", "grey50"))
dev.off()

pdf(paste("danger_scale_comp.pdf"),w=6,h=5)
sm.density.compare(subsample_graph_danger$danger_graph, comply_danger, xlim=c(1, 6), xlab="likes danger: 1=unlike me, 6=like me", col=c("blue", "dodgerblue1", "black", "grey50"), lty=c(1, 2, 1, 2))
title(main="Distribution of danger-loving personality")
legend("bottomright", levels(comply_danger), fill=c("blue", "dodgerblue1", "black", "grey50"))

dev.off()


#Section 5

subsample2 <- read.dta13("data_fb.dta")


#Recode

subsample2$uni[subsample2$edu_1==1] <- 1
subsample2$uni[subsample2$edu_2==1] <- 0
subsample2$uni[subsample2$edu_3==1] <- 0
subsample2$uni[subsample2$edu_4==1] <- 0
subsample2$uni[subsample2$edu_5==1] <- 0


subsample2$status_per[subsample2$status_1==1] <- 1
subsample2$status_per[subsample2$status_2==1] <- 1
subsample2$status_per[subsample2$status_3==1] <- 0
subsample2$status_per[subsample2$status_4==1] <- 0
subsample2$status_per[subsample2$status_5==1] <- 0


subsample_var2<-subset(subsample2, select=c(knowledge_post, seen_logo, chances, importance_self, appreciate, difference, discuss_online, discuss_real, protest, assignment, compliance, civic, fbtreat, fbvsnewsletter, stream, townpoplog, age_re, sex_2, uni, edu_5, status_per, status_5, friends_re, fbfriends_re, frequency_fb, news_2, news_3,  civic_re, environment_act_2, vote_re, scale_trust, role_2, role_3, knowledgescale, nature_re, danger, adventure, pleasure, ability))

subsample_var2$fbcomply[subsample_var2$assignment==1] <- 0
subsample_var2$fbcomply[subsample_var2$assignment==2 & subsample_var2$compliance==1] <- 1
subsample_var2$fbcomply[subsample_var2$assignment==2 & subsample_var2$compliance==0] <- 0
subsample_var2$fbcomply[subsample_var2$assignment==3 & subsample_var2$compliance==0] <- 0
subsample_var2$fbcomply[subsample_var2$assignment==3 & subsample_var2$compliance==1] <- 0

table(subsample_var2$compliance[subsample_var2$assignment!=2])
table(subsample_var2$compliance[subsample_var2$assignment!=3])

subsample_var2$fbtreat[subsample_var2$assignment==1] <- 0
subsample_var2$fbtreat[subsample_var2$assignment==2] <- 1
subsample_var2$fbtreat[subsample_var2$assignment==3] <- 0

subsample_var2$newsletter[subsample_var2$assignment==1] <- 0
subsample_var2$newsletter[subsample_var2$assignment==2] <- 0
subsample_var2$newsletter[subsample_var2$assignment==3] <- 1

subsample_var2$newscomply[subsample_var2$assignment==1] <- 0
subsample_var2$newscomply[subsample_var2$assignment==2 & subsample_var2$compliance==1] <- 0
subsample_var2$newscomply[subsample_var2$assignment==2 & subsample_var2$compliance==0] <- 0
subsample_var2$newscomply[subsample_var2$assignment==3 & subsample_var2$compliance==0] <- 0
subsample_var2$newscomply[subsample_var2$assignment==3 & subsample_var2$compliance==1] <- 1

#Discuss Online

subsample_discuss_online<-subset(subsample_var2,!is.na(discuss_online))

subsample_discuss_online$sdY<-sd(subsample_discuss_online$discuss_online[subsample_discuss_online$assignment==1])
subsample_discuss_online$muY<-mean(subsample_discuss_online$discuss_online[subsample_discuss_online$assignment==1])
subsample_discuss_online$Y <- subsample_discuss_online$discuss_online 

subsample_discuss_online$Yscaled <- (subsample_discuss_online$Y-subsample_discuss_online$muY)/subsample_discuss_online$sdY # define Outcome

subsample_discuss_online$Z <- subsample_discuss_online$assignment # define Treatment Assignment
subsample_discuss_online$X<- as.matrix(subsample_discuss_online[,16:39])
subsample_discuss_online$Z1 <- subsample_discuss_online$fbtreat # define Treatment FB Assignment
subsample_discuss_online$Z2 <- subsample_discuss_online$newsletter # define Treatment Newsletter Assignment
subsample_discuss_online$D1 <- subsample_discuss_online$fbcomply # define Compliance
subsample_discuss_online$D2 <- subsample_discuss_online$newscomply # define Compliance


subsample_discuss_online$blocks[subsample_discuss_online$stream=="representative"] <-0
subsample_discuss_online$blocks[subsample_discuss_online$stream=="activists"] <-1 

subsample_discuss_online_CACEfb<- subset(subsample_discuss_online,!is.na(Z1))
subsample_discuss_online_CACEnl<- subset(subsample_discuss_online,!is.na(Z2))

Z<-subsample_discuss_online$Z

blocks<-subsample_discuss_online$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(117,92, 122),
                      c(64, 57, 48))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_discuss_online$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)

subsample_discuss_online_CACEfb<- subset(subsample_discuss_online,!is.na(Z1))
subsample_discuss_online_CACEnl<- subset(subsample_discuss_online,!is.na(Z2))


#Discuss Offline

subsample_discuss_real<-subset(subsample_var2,!is.na(discuss_real))

subsample_discuss_real$Y <- subsample_discuss_real$discuss_real  # define Outcome

subsample_discuss_real$sdY<-sd(subsample_discuss_real$discuss_real[subsample_discuss_real$assignment==1])
subsample_discuss_real$muY<-mean(subsample_discuss_real$discuss_real[subsample_discuss_real$assignment==1])

subsample_discuss_real$Yscaled <- (subsample_discuss_real$discuss_real-subsample_discuss_real$muY)/subsample_discuss_real$sdY # define Outcome

subsample_discuss_real$Z <- subsample_discuss_real$assignment # define Treatment Assignment
subsample_discuss_real$Z1 <- subsample_discuss_real$fbtreat # define Treatment FB Assignment
subsample_discuss_real$Z2 <- subsample_discuss_real$newsletter # define Treatment Newsletter Assignment
subsample_discuss_real$D1 <- subsample_discuss_real$fbcomply # define Compliance
subsample_discuss_real$D2 <- subsample_discuss_real$newscomply # define Compliance
subsample_discuss_real$X<- as.matrix(subsample_discuss_real[,16:39])

subsample_discuss_real$blocks[subsample_discuss_real$stream=="representative"] <-0
subsample_discuss_real$blocks[subsample_discuss_real$stream=="activists"] <-1

#ITT

Z<-subsample_discuss_real$Z

blocks<-subsample_discuss_real$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(132,106, 145),
                      c(66, 60, 50))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_discuss_real$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)

subsample_discuss_real_CACEfb<- subset(subsample_discuss_real,!is.na(Z1))
subsample_discuss_real_CACEnl<- subset(subsample_discuss_real,!is.na(Z2))


#Others Appreciate 

subsample_appreciate<-subset(subsample_var2,!is.na(appreciate))

subsample_appreciate$sdY<-sd(subsample_appreciate$appreciate[subsample_appreciate$assignment==1])
subsample_appreciate$muY<-mean(subsample_appreciate$appreciate[subsample_appreciate$assignment==1])
subsample_appreciate$Y <- subsample_appreciate$appreciate 
subsample_appreciate$Yscaled <- (subsample_appreciate$Y-subsample_appreciate$muY)/subsample_appreciate$sdY # define Outcome

subsample_appreciate$Z <- subsample_appreciate$assignment # define Treatment Assignment
subsample_appreciate$Z1 <- subsample_appreciate$fbtreat # define FB Treatment Assignment
subsample_appreciate$Z2 <- subsample_appreciate$newsletter # define Newsletter Treatment Assignment
subsample_appreciate$D1 <- subsample_appreciate$fbcomply # define Compliance
subsample_appreciate$D2 <- subsample_appreciate$newscomply # define Compliance
subsample_appreciate$X<- as.matrix(subsample_appreciate[,16:39])

subsample_appreciate$blocks[subsample_appreciate$stream=="representative"] <-0
subsample_appreciate$blocks[subsample_appreciate$stream=="activists"] <-1

Z<-subsample_appreciate$Z

blocks<-subsample_appreciate$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(133,108, 146),
                      c(66, 60, 49))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_appreciate$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)


subsample_appreciate_CACEfb<- subset(subsample_appreciate,!is.na(Z1))
subsample_appreciate_CACEnl<- subset(subsample_appreciate,!is.na(Z2))



#Chances

subsample_chances<-subset(subsample_var2,!is.na(chances))

subsample_chances$sdY<-sd(subsample_chances$chances[subsample_chances$assignment==1])
subsample_chances$muY<-mean(subsample_chances$chances[subsample_chances$assignment==1])
subsample_chances$Y <- subsample_chances$chances 
subsample_chances$Yscaled <- (subsample_chances$Y-subsample_chances$muY)/subsample_chances$sdY # define Outcome


subsample_chances$Z <- subsample_chances$assignment # define Treatment Assignment
subsample_chances$Z1 <- subsample_chances$fbtreat # define FB Treatment Assignment
subsample_chances$Z2 <- subsample_chances$newsletter # define Newsletter Treatment Assignment
subsample_chances$D1 <- subsample_chances$fbcomply # define Compliance
subsample_chances$D2 <- subsample_chances$newscomply # define Compliance
subsample_chances$X<- as.matrix(subsample_chances[,16:39])

subsample_chances$blocks[subsample_chances$stream=="representative"] <-0
subsample_chances$blocks[subsample_chances$stream=="activists"] <-1

Z<-subsample_chances$Z

blocks<-subsample_chances$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(133,109, 147),
                      c(67, 61, 50))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_chances$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)

subsample_chances_CACEfb<- subset(subsample_chances,!is.na(Z1))
subsample_chances_CACEnl<- subset(subsample_chances,!is.na(Z2))


#Difference

subsample_difference<-subset(subsample_var2,!is.na(difference))

subsample_difference$sdY<-sd(subsample_difference$difference[subsample_difference$assignment==1])
subsample_difference$muY<-mean(subsample_difference$difference[subsample_difference$assignment==1])
subsample_difference$Y <- subsample_difference$difference
subsample_difference$Yscaled <- (subsample_difference$Y-subsample_difference$muY)/subsample_difference$sdY # define Outcome

subsample_difference$Z <- subsample_difference$assignment# define Treatment Assignment
subsample_difference$Z1 <- subsample_difference$fbtreat # define FB Treatment Assignment
subsample_difference$Z2 <- subsample_difference$newsletter # define Newsletter Treatment Assignment
subsample_difference$D1 <- subsample_difference$fbcomply # define Compliance
subsample_difference$D2 <- subsample_difference$newscomply # define Compliance
subsample_difference$X<- as.matrix(subsample_difference[,16:39])

subsample_difference$blocks[subsample_difference$stream=="representative"] <-0
subsample_difference$blocks[subsample_difference$stream=="activists"] <-1

Z<-subsample_difference$Z

blocks<-subsample_difference$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(133,108, 147),
                      c(67, 60, 50))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_difference$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)


subsample_difference_CACEfb<- subset(subsample_difference,!is.na(Z1))
subsample_difference_CACEnl<- subset(subsample_difference,!is.na(Z2))


#Importance Environment

subsample_importance<-subset(subsample_var2,!is.na(importance_self))

subsample_importance$sdY<-sd(subsample_importance$importance_self[subsample_importance$assignment==1])
subsample_importance$muY<-mean(subsample_importance$importance_self[subsample_importance$assignment==1])
subsample_importance$Y <- subsample_importance$importance_self 
subsample_importance$Yscaled <- (subsample_importance$Y-subsample_importance$muY)/subsample_importance$sdY # define Outcome

subsample_importance$Z <- subsample_importance$assignment # define Treatment Assignment
subsample_importance$Z1 <- subsample_importance$fbtreat # define FB Treatment Assignment
subsample_importance$Z2 <- subsample_importance$newsletter # define Newsletter Treatment Assignment
subsample_importance$D1 <- subsample_importance$fbcomply # define Compliance
subsample_importance$D2 <- subsample_importance$newscomply # Compliance
subsample_importance$X<- as.matrix(subsample_importance[,16:39])

subsample_importance$blocks[subsample_importance$stream=="representative"] <-0
subsample_importance$blocks[subsample_importance$stream=="activists"] <-1

Z<-subsample_importance$Z

blocks<-subsample_importance$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(132,106, 143),
                      c(66, 60, 48))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_importance$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)

subsample_importance_CACEfb<- subset(subsample_importance,!is.na(Z1))
subsample_importance_CACEnl<- subset(subsample_importance,!is.na(Z2))



#Knowledge

subsample_knowledge<-subset(subsample_var2,!is.na(knowledge_post))

subsample_knowledge$sdY<-sd(subsample_knowledge$knowledge_post[subsample_knowledge$assignment==1] )
subsample_knowledge$muY<-mean(subsample_knowledge$knowledge_post[subsample_knowledge$assignment==1])
subsample_knowledge$Y <- subsample_knowledge$knowledge_post  
subsample_knowledge$Yscaled <- (subsample_knowledge$Y-subsample_knowledge$muY)/subsample_knowledge$sdY # define Outcome

subsample_knowledge$Z <- subsample_knowledge$assignment# define Treatment Assignment
subsample_knowledge$Z1 <- subsample_knowledge$fbtreat # define FB Treatment Assignment
subsample_knowledge$Z2 <- subsample_knowledge$newsletter # define Newsletter Treatment Assignment
subsample_knowledge$D1 <- subsample_knowledge$fbcomply # define Compliance
subsample_knowledge$D2 <- subsample_knowledge$newscomply # define Compliance
subsample_knowledge$X<- as.matrix(subsample_knowledge[,16:39])

subsample_knowledge$blocks[subsample_knowledge$stream=="representative"] <-0
subsample_knowledge$blocks[subsample_knowledge$stream=="activists"] <-1

Z<-subsample_knowledge$Z

blocks<-subsample_knowledge$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(133,108, 147),
                      c(67, 60, 50))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_knowledge$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)


subsample_knowledge_CACEfb<- subset(subsample_knowledge,!is.na(Z1))
subsample_knowledge_CACEnl<- subset(subsample_knowledge,!is.na(Z2))



#Protest

subsample_protest<-subset(subsample_var2,!is.na(protest))

subsample_protest$Y <- subsample_protest$protest  
subsample_protest$sdY<-sd(subsample_protest$protest[subsample_protest$assignment==1] )
subsample_protest$muY<-mean(subsample_protest$protest[subsample_protest$assignment==1])
subsample_protest$Yscaled <- (subsample_protest$Y-subsample_protest$muY)/subsample_protest$sdY # define Outcome

subsample_protest$Z <- subsample_protest$assignment# define Treatment Assignment
subsample_protest$Z1 <- subsample_protest$fbtreat # define FB Treatment Assignment
subsample_protest$Z2 <- subsample_protest$newsletter # define Newsletter Treatment Assignment
subsample_protest$D1 <- subsample_protest$fbcomply # define Compliance
subsample_protest$D2 <- subsample_protest$newscomply # define Compliance
subsample_protest$X<- as.matrix(subsample_protest[,16:39])

subsample_protest$blocks[subsample_protest$stream=="representative"] <-0
subsample_protest$blocks[subsample_protest$stream=="activists"] <-1

Z<-subsample_protest$Z

blocks<-subsample_protest$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(133,106, 146),
                      c(67, 60, 50))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_protest$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)


subsample_protest_CACEfb<- subset(subsample_protest,!is.na(Z1))
subsample_protest_CACEnl<- subset(subsample_protest,!is.na(Z2))



#Manipulation Check

subsample_logo<-subset(subsample_var2,!is.na(seen_logo))

subsample_logo$Y <- subsample_logo$seen_logo # define Outcome
subsample_logo$Z <- subsample_logo$assignment# define Treatment Assignment
subsample_logo$Z1 <- subsample_logo$fbtreat # define FB Treatment Assignment
subsample_logo$Z2 <- subsample_logo$newsletter # define Newsletter Treatment Assignment
subsample_logo$D1 <- subsample_logo$fbcomply # define Compliance
subsample_logo$D2 <- subsample_logo$newscomply # define Compliance
subsample_logo$X<- as.matrix(subsample_logo[,16:39])


subsample_logo$blocks[subsample_logo$stream=="representative"] <-0
subsample_logo$blocks[subsample_logo$stream=="activists"] <-1

mean(subsample_logo$Y[subsample_logo$assignment==1])
mean(subsample_logo$Y[subsample_logo$assignment==2])
mean(subsample_logo$Y[subsample_logo$assignment==3])

Z<-subsample_logo$Z

blocks<-subsample_logo$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(blocks, assignment)

block_m_each <- rbind(c(132,107, 148),
                      c(66, 60, 50))

Blocked_design <- declare_ra(blocks=blocks, block_m_each=block_m_each)
subsample_logo$prob_obs <- obtain_condition_probabilities(Blocked_design, assignment=assignment)

subsample_logo_CACEfb<- subset(subsample_logo,!is.na(Z1))
subsample_logo_CACEnl<- subset(subsample_logo,!is.na(Z2))


#Figure 3

tab_logo<-NA

tab_logo <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_logo$ITT<- NA
tab_logo$Cilower <- NA
tab_logo$Ciupper <- NA
tab_logo$covariates <- NA

colnames(tab_logo) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "model")

lm_logo_unadj_tab <- lm(Y~factor(Z), weight=1/prob_obs, data=subsample_logo)

lm_logo_adj_tab <- lm(Y~factor(Z) + X,  weight=1/prob_obs, data=subsample_logo)


lm_logo_unadj <- lm_robust(Y~factor(Z), weight=1/prob_obs, data=subsample_logo)

lm_logo_adj <- lm_robust(Y~factor(Z) + X, weight=1/prob_obs, data=subsample_logo)

tab_logo[1,2] <- coef(lm_logo_adj)[2]
tab_logo[2,2] <- coef(lm_logo_adj)[3]
tab_logo[3,2] <- coef(lm_logo_unadj)[2]
tab_logo[4,2] <- coef(lm_logo_unadj)[3]

tab_logo[1,3]<-lm_logo_adj$conf.low[2]
tab_logo[1,4]<-lm_logo_adj$conf.high[2]

tab_logo[2,3]<-lm_logo_adj$conf.low[3]
tab_logo[2,4]<-lm_logo_adj$conf.high[3]

tab_logo[3,3]<-lm_logo_unadj$conf.low[2]
tab_logo[3,4]<-lm_logo_unadj$conf.high[2]

tab_logo[4,3]<-lm_logo_unadj$conf.low[3]
tab_logo[4,4]<-lm_logo_unadj$conf.high[3]

tab_logo[1,5]<-"unadjusted"
tab_logo[2,5]<-"unadjusted"
tab_logo[3,5]<-"covariate-adjusted"
tab_logo[4,5]<-"covariate-adjusted"

#Figure 3

pdf(paste("results_logo.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_logo,aes(x = model, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on recall of campaign logo") + coord_cartesian(ylim = c(-0.1, 0.3)) +  ylab("ITT (in pp)")
dev.off()  



#Discussion online

#ITT

tab_social_interaction<-NA

tab_social_interaction <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_social_interaction$ITT<- NA
tab_social_interaction$Cilower <- NA
tab_social_interaction$Ciupper <- NA
tab_social_interaction$outcome <- NA

colnames(tab_social_interaction) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")

lm_discussonline_unadj_tab <- lm(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_discuss_online)

lm_discussonline_adj_tab <- lm(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_discuss_online)

lm_discussonline_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_discuss_online)

lm_discussonline_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_discuss_online)

tab_social_interaction[1,2] <- coef(lm_discussonline_adj)[2]
tab_social_interaction[2,2] <- coef(lm_discussonline_adj)[3]

tab_social_interaction[1,3]<-lm_discussonline_adj$conf.low[2]
tab_social_interaction[1,4]<-lm_discussonline_adj$conf.high[2]

tab_social_interaction[2,3]<-lm_discussonline_adj$conf.low[3]
tab_social_interaction[2,4]<-lm_discussonline_adj$conf.high[3]



#Discussion offline

#ITT

lm_discussoffline_unadj_tab <- lm(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_discuss_real)

lm_discussoffline_adj_tab <- lm(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_discuss_real)

lm_discussoffline_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_discuss_real)

lm_discussoffline_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_discuss_real)

tab_social_interaction[3,2] <- coef(lm_discussoffline_adj)[2]
tab_social_interaction[4,2] <- coef(lm_discussoffline_adj)[3]

tab_social_interaction[3,3]<-lm_discussoffline_adj$conf.low[2]
tab_social_interaction[3,4]<-lm_discussoffline_adj$conf.high[2]

tab_social_interaction[4,3]<-lm_discussoffline_adj$conf.low[3]
tab_social_interaction[4,4]<-lm_discussoffline_adj$conf.high[3]


tab_social_interaction[3,2] <- coef(lm_discussoffline_adj)[2]
tab_social_interaction[4,2] <- coef(lm_discussoffline_adj)[3]

tab_social_interaction[3,3]<-lm_discussoffline_adj$conf.low[2]
tab_social_interaction[3,4]<-lm_discussoffline_adj$conf.high[2]

tab_social_interaction[4,3]<-lm_discussoffline_adj$conf.low[3]
tab_social_interaction[4,4]<-lm_discussoffline_adj$conf.high[3]



#Appreciated

#ITT

lm_appreciate_unadj_tab <- lm(Yscaled~factor(Z), weight=1/prob_obs,data=subsample_appreciate)

lm_appreciate_adj_tab <- lm(Yscaled~factor(Z) + X, weight=1/prob_obs,data=subsample_appreciate)

lm_appreciate_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_appreciate)

lm_appreciate_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_appreciate)


tab_social_interaction[5,2] <- coef(lm_appreciate_adj)[2]
tab_social_interaction[6,2] <- coef(lm_appreciate_adj)[3]

tab_social_interaction[5,3]<-lm_appreciate_adj$conf.low[2]
tab_social_interaction[5,4]<-lm_appreciate_adj$conf.high[2]

tab_social_interaction[6,3]<-lm_appreciate_adj$conf.low[3]
tab_social_interaction[6,4]<-lm_appreciate_adj$conf.high[3]

tab_social_interaction[1,5]<-"Discuss Online"
tab_social_interaction[2,5]<-"Discuss Online"
tab_social_interaction[3,5]<-"Discuss Offline"
tab_social_interaction[4,5]<-"Discuss Offline"
tab_social_interaction[5,5]<-"Feeling appreciated"
tab_social_interaction[6,5]<-"Feeling appreciated"

#Figure 4

pdf(paste("results_social_interaction.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_social_interaction,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on social interaction") + coord_cartesian(ylim = c(-0.5, 0.5)) +  ylab("ITT (in sd)")
dev.off() 



#Chances

#ITT


tab_outcomes<-NA

tab_outcomes <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_outcomes$ITT<- NA
tab_outcomes$Cilower <- NA
tab_outcomes$Ciupper <- NA
tab_outcomes$outcome <- NA

colnames(tab_outcomes) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")
dim(tab_outcomes)

tab_outcomes_CACE<-NA

tab_outcomes_CACE <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_outcomes_CACE$CACE<- NA
tab_outcomes_CACE$Cilower <- NA
tab_outcomes_CACE$Ciupper <- NA
tab_outcomes_CACE$outcome <- NA

colnames(tab_outcomes_CACE) <- c("Treatment", "CACE","Ci_lower", "Ci_upper", "Outcome")

lm_chances_unadj_tab <- lm(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_chances)

lm_chances_adj_tab <- lm(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_chances)

lm_chances_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_chances)

lm_chances_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_chances)

tab_outcomes[1,2] <- coef(lm_chances_adj)[2]
tab_outcomes[2,2] <- coef(lm_chances_adj)[3]

tab_outcomes[1,3]<-lm_chances_adj$conf.low[2]
tab_outcomes[1,4]<-lm_chances_adj$conf.high[2]

tab_outcomes[2,3]<-lm_chances_adj$conf.low[3]
tab_outcomes[2,4]<-lm_chances_adj$conf.high[3]


#Likelihood of protest

#ITT

lm_protest_unadj_tab <- lm(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_protest)

lm_protest_adj_tab <- lm(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_protest)

lm_protest_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_protest)

lm_protest_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_protest)

tab_outcomes[5,2] <- coef(lm_protest_adj)[2]
tab_outcomes[6,2] <- coef(lm_protest_adj)[3]

tab_outcomes[5,3]<-lm_protest_adj$conf.low[2]
tab_outcomes[5,4]<-lm_protest_adj$conf.high[2]

tab_outcomes[6,3]<-lm_protest_adj$conf.low[3]
tab_outcomes[6,4]<-lm_protest_adj$conf.high[3]


#Make a difference

#ITT

lm_difference_unadj_tab <- lm(Yscaled~factor(Z) , weight=1/prob_obs, data=subsample_difference)

lm_difference_adj_tab <- lm(Yscaled~factor(Z) + X  , weight=1/prob_obs, data=subsample_difference)

lm_difference_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_difference)

lm_difference_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_difference)

tab_outcomes[3,2] <- coef(lm_difference_adj)[2]
tab_outcomes[4,2] <- coef(lm_difference_adj)[3]

tab_outcomes[3,3]<-lm_difference_adj$conf.low[2]
tab_outcomes[3,4]<-lm_difference_adj$conf.high[2]

tab_outcomes[4,3]<-lm_difference_adj$conf.low[3]
tab_outcomes[4,4]<-lm_difference_adj$conf.high[3]

tab_outcomes[1,5]<-"Chances of success"
tab_outcomes[2,5]<-"Chances of success"
tab_outcomes[3,5]<-"Make a difference"
tab_outcomes[4,5]<-"Make a difference"
tab_outcomes[5,5]<-"Likelihood of protest"
tab_outcomes[6,5]<-"Likelihood of protest"


#Figure 5

pdf(paste("results_outcomes.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_outcomes,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on perceptions of activism") + coord_cartesian(ylim = c(-0.5, 0.5)) +  ylab("ITT (in sd)")
dev.off() 



#Environment

#ITT

tab_attitudes<-NA

tab_attitudes <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_attitudes$ITT<- NA
tab_attitudes$Cilower <- NA
tab_attitudes$Ciupper <- NA
tab_attitudes$outcome <- NA

colnames(tab_attitudes) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")

lm_importance_unadj_tab <- lm(Yscaled~factor(Z), weight=1/prob_obs,data=subsample_importance)

lm_importance_adj_tab <- lm(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_importance)

lm_importance_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_importance)

lm_importance_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_importance)

tab_attitudes[1,2] <- coef(lm_importance_adj)[2]
tab_attitudes[2,2] <- coef(lm_importance_adj)[3]

tab_attitudes[1,3]<-lm_importance_adj$conf.low[2]
tab_attitudes[1,4]<-lm_importance_adj$conf.high[2]

tab_attitudes[2,3]<-lm_importance_adj$conf.low[3]
tab_attitudes[2,4]<-lm_importance_adj$conf.high[3]


#Knowledge

#ITT

lm_knowledge_unadj_tab <- lm(Yscaled~factor(Z),  weight=1/prob_obs,data=subsample_knowledge)

lm_knowledge_adj_tab <- lm(Yscaled~factor(Z) + X,  weight=1/prob_obs,data=subsample_knowledge)

lm_knowledge_unadj <- lm_robust(Yscaled~factor(Z), weight=1/prob_obs, data=subsample_knowledge)

lm_knowledge_adj <- lm_robust(Yscaled~factor(Z) + X, weight=1/prob_obs, data=subsample_knowledge)


tab_attitudes[3,2] <- coef(lm_knowledge_adj)[2]
tab_attitudes[4,2] <- coef(lm_knowledge_adj)[3]

tab_attitudes[3,3]<-lm_knowledge_adj$conf.low[2]
tab_attitudes[3,4]<-lm_knowledge_adj$conf.high[2]

tab_attitudes[4,3]<-lm_knowledge_adj$conf.low[3]
tab_attitudes[4,4]<-lm_knowledge_adj$conf.high[3]

tab_attitudes[1,5]<-"perceived importance environment"
tab_attitudes[2,5]<-"perceived importance environment"
tab_attitudes[3,5]<-"environmental knowledge"
tab_attitudes[4,5]<-"environmental knowledge"


#Figure 6

pdf(paste("results_attitudes.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_attitudes,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on environmental attitudes/knowledge") + coord_cartesian(ylim = c(-0.5, 0.5)) +  ylab("ITT (in sd)")
dev.off() 



#Figure 7


#CACE


CACE_discussonline_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_discuss_online)

CACE_discussonline_adj <- iv_robust(Yscaled ~ D1 + D2 + X | Z1 + Z2+ X, weight=1/prob_obs, data=subsample_discuss_online)


CACE_discussonline_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_discuss_online)

CACE_discussonline_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X | Z1 + Z2+ X, weight=1/prob_obs, data=subsample_discuss_online)


CACE_discussonline_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_discuss_online_CACEfb)

CACE_discussonline_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_discuss_online_CACEnl)


CACE_discussonline_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_discuss_online_CACEfb)

CACE_discussonline_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_discuss_online_CACEnl)


tab_social_interaction_CACE<-NA

tab_social_interaction_CACE <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_social_interaction_CACE$CACE<- NA
tab_social_interaction_CACE$Cilower <- NA
tab_social_interaction_CACE$Ciupper <- NA
tab_social_interaction_CACE$outcome <- NA

colnames(tab_social_interaction_CACE) <- c("Treatment", "CACE","Ci_lower", "Ci_upper", "Outcome")

tab_social_interaction_CACE[1,2] <- coef(CACE_discussonline_adj)[2]
tab_social_interaction_CACE[2,2] <- coef(CACE_discussonline_adj)[3]

tab_social_interaction_CACE[1,3]<-CACE_discussonline_adj$conf.low[2]
tab_social_interaction_CACE[1,4]<-CACE_discussonline_adj$conf.high[2]

tab_social_interaction_CACE[2,3]<-CACE_discussonline_adj$conf.low[3]
tab_social_interaction_CACE[2,4]<-CACE_discussonline_adj$conf.high[3]


CACE_discussoffline_unadj <- iv_robust(Yscaled ~ D1 + D2 | Z1 + Z2, weight=1/prob_obs, data=subsample_discuss_real)

CACE_discussoffline_adj <- iv_robust(Yscaled ~ D1 + D2 + X | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_discuss_real)


CACE_discussoffline_unadj_tab <- ivreg(Yscaled ~ D1 + D2 | Z1 + Z2, weight=1/prob_obs, data=subsample_discuss_real)

CACE_discussoffline_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_discuss_real)


CACE_discussoffline_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_discuss_real_CACEfb)

CACE_discussoffline_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_discuss_real_CACEnl)


CACE_discussoffline_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_discuss_real_CACEfb)

CACE_discussoffline_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_discuss_real_CACEnl)


tab_social_interaction_CACE[3,2] <- coef(CACE_discussoffline_adj)[2]
tab_social_interaction_CACE[4,2] <- coef(CACE_discussoffline_adj)[3]

tab_social_interaction_CACE[3,3]<-CACE_discussoffline_adj$conf.low[2]
tab_social_interaction_CACE[3,4]<-CACE_discussoffline_adj$conf.high[2]

tab_social_interaction_CACE[4,3]<-CACE_discussoffline_adj$conf.low[3]
tab_social_interaction_CACE[4,4]<-CACE_discussoffline_adj$conf.high[3]


CACE_appreciate_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_appreciate)

CACE_appreciate_adj <- iv_robust(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_appreciate)

CACE_appreciate_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_appreciate)

CACE_appreciate_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_appreciate)

CACE_appreciate_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_appreciate_CACEfb)
CACE_appreciate_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_appreciate_CACEnl)

CACE_appreciate_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_appreciate_CACEfb)

CACE_appreciate_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_appreciate_CACEnl)

tab_social_interaction_CACE[5,2] <- coef(CACE_appreciate_adj)[2]
tab_social_interaction_CACE[6,2] <- coef(CACE_appreciate_adj)[3]

tab_social_interaction_CACE[5,3]<-CACE_appreciate_adj$conf.low[2]
tab_social_interaction_CACE[5,4]<-CACE_appreciate_adj$conf.high[2]

tab_social_interaction_CACE[6,3]<-CACE_appreciate_adj$conf.low[3]
tab_social_interaction_CACE[6,4]<-CACE_appreciate_adj$conf.high[3]

tab_social_interaction_CACE[1,5]<-"Discuss Online"
tab_social_interaction_CACE[2,5]<-"Discuss Online"
tab_social_interaction_CACE[3,5]<-"Discuss Offline"
tab_social_interaction_CACE[4,5]<-"Discuss Offline"
tab_social_interaction_CACE[5,5]<-"Feeling appreciated"
tab_social_interaction_CACE[6,5]<-"Feeling appreciated"


CACE_chances_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_chances)

CACE_chances_adj <- iv_robust(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_chances)


CACE_chances_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_chances)

CACE_chances_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_chances)


CACE_chances_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_chances)

CACE_chances_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_chances)


CACE_chances_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_chances)

CACE_chances_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_chances)


tab_outcomes_CACE[1,2] <- coef(CACE_chances_adj)[2]
tab_outcomes_CACE[2,2] <- coef(CACE_chances_adj)[3]

tab_outcomes_CACE[1,3]<-CACE_chances_adj$conf.low[2]
tab_outcomes_CACE[1,4]<-CACE_chances_adj$conf.high[2]

tab_outcomes_CACE[2,3]<-CACE_chances_adj$conf.low[3]
tab_outcomes_CACE[2,4]<-CACE_chances_adj$conf.high[3]


CACE_protest_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_protest)

CACE_protest_adj <- iv_robust(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_protest)


CACE_protest_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_protest)

CACE_protest_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_protest)


CACE_protest_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_protest)

CACE_protest_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_protest)


CACE_protest_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_protest)

CACE_protest_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_protest)


tab_outcomes_CACE[5,2] <- coef(CACE_protest_adj)[2]
tab_outcomes_CACE[6,2] <- coef(CACE_protest_adj)[3]

tab_outcomes_CACE[5,3]<-CACE_protest_adj$conf.low[2]
tab_outcomes_CACE[5,4]<-CACE_protest_adj$conf.high[2]

tab_outcomes_CACE[6,3]<-CACE_protest_adj$conf.low[3]
tab_outcomes_CACE[6,4]<-CACE_protest_adj$conf.high[3]


CACE_difference_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_difference)

CACE_difference_adj <- iv_robust(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_difference)


CACE_difference_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_difference)

CACE_difference_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_difference)


CACE_difference_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_difference)

CACE_difference_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_difference)


CACE_difference_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_difference)

CACE_difference_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_difference)


tab_outcomes_CACE[3,2] <- coef(CACE_difference_adj)[2]
tab_outcomes_CACE[4,2] <- coef(CACE_difference_adj)[3]

tab_outcomes_CACE[3,3]<-CACE_difference_adj$conf.low[2]
tab_outcomes_CACE[3,4]<-CACE_difference_adj$conf.high[2]

tab_outcomes_CACE[4,3]<-CACE_difference_adj$conf.low[3]
tab_outcomes_CACE[4,4]<-CACE_difference_adj$conf.high[3]

tab_outcomes_CACE[1,5]<-"Chances of success"
tab_outcomes_CACE[2,5]<-"Chances of success"
tab_outcomes_CACE[3,5]<-"Make a difference"
tab_outcomes_CACE[4,5]<-"Make a difference"
tab_outcomes_CACE[5,5]<-"Likelihood of protest"
tab_outcomes_CACE[6,5]<-"Likelihood of protest"


tab_attitudes_CACE<-NA

tab_attitudes_CACE <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_attitudes_CACE$CACE<- NA
tab_attitudes_CACE$Cilower <- NA
tab_attitudes_CACE$Ciupper <- NA
tab_attitudes_CACE$outcome <- NA

colnames(tab_attitudes_CACE) <- c("Treatment", "CACE","Ci_lower", "Ci_upper", "Outcome")
dim(tab_attitudes_CACE)

CACE_importance_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_importance)

CACE_importance_adj <- iv_robust(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_importance)


CACE_importance_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_importance)

CACE_importance_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_importance)


CACE_importance_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_importance)

CACE_importance_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_importance)


CACE_importance_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_importance)

CACE_importance_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_importance)



tab_attitudes_CACE[1,2] <- coef(CACE_importance_adj)[2]
tab_attitudes_CACE[2,2] <- coef(CACE_importance_adj)[3]

tab_attitudes_CACE[1,3]<-CACE_importance_adj$conf.low[2]
tab_attitudes_CACE[1,4]<-CACE_importance_adj$conf.high[2]

tab_attitudes_CACE[2,3]<-CACE_importance_adj$conf.low[3]
tab_attitudes_CACE[2,4]<-CACE_importance_adj$conf.high[3]



CACE_knowledge_unadj <- iv_robust(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_knowledge)

CACE_knowledge_adj <- iv_robust(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_knowledge)


CACE_knowledge_unadj_tab <- ivreg(Yscaled ~ D1 + D2  | Z1 + Z2, weight=1/prob_obs, data=subsample_knowledge)

CACE_knowledge_adj_tab <- ivreg(Yscaled ~ D1 + D2 + X  | Z1 + Z2 + X, weight=1/prob_obs, data=subsample_knowledge)


CACE_knowledge_unadj_fb_tab <- ivreg(Yscaled ~ D1  | Z1 , weight=1/prob_obs, data=subsample_knowledge)

CACE_knowledge_unadj_nl_tab <- ivreg(Yscaled ~ D2 | Z2 , weight=1/prob_obs, data=subsample_knowledge)


CACE_knowledge_adj_fb_tab <- ivreg(Yscaled ~ D1 + X | Z1 + X , weight=1/prob_obs, data=subsample_knowledge)

CACE_knowledge_adj_nl_tab <- ivreg(Yscaled ~ D2 + X | Z2 + X , weight=1/prob_obs, data=subsample_knowledge)


tab_attitudes_CACE[3,2] <- coef(CACE_knowledge_adj)[2]
tab_attitudes_CACE[4,2] <- coef(CACE_knowledge_adj)[3]

tab_attitudes_CACE[3,3]<-CACE_knowledge_adj$conf.low[2]
tab_attitudes_CACE[3,4]<-CACE_knowledge_adj$conf.high[2]

tab_attitudes_CACE[4,3]<-CACE_knowledge_adj$conf.low[3]
tab_attitudes_CACE[4,4]<-CACE_knowledge_adj$conf.high[3]

tab_attitudes_CACE[1,5]<-"perceived importance environment"
tab_attitudes_CACE[2,5]<-"perceived importance environment"
tab_attitudes_CACE[3,5]<-"environmental knowledge"
tab_attitudes_CACE[4,5]<-"environmental knowledge"



#Figure 7


pdf(paste("results_social_interaction_CACE.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_social_interaction_CACE,aes(x = Outcome, y = CACE,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("CACE on social interaction") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("CACE (in sd)")
dev.off() 

pdf(paste("results_outcomes_CACE.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_outcomes_CACE,aes(x = Outcome, y = CACE,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("CACE on perceptions of activism") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("CACE (in sd)")
dev.off() 

pdf(paste("results_attitudes_CACE.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_attitudes_CACE,aes(x = Outcome, y = CACE,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("CACE on environmental attitudes/knowledge") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("CACE (in sd)")
dev.off() 


###################

#Figure 8

subsample3 <- read.dta13("data_fb.dta")

subsample3$rep[subsample3$stream=="representative"] <-1
subsample3$act[subsample3$stream=="activists"] <-1

subsample3$uni[subsample3$edu_1==1] <- 1
subsample3$uni[subsample3$edu_2==1] <- 0
subsample3$uni[subsample3$edu_3==1] <- 0
subsample3$uni[subsample3$edu_4==1] <- 0
subsample3$uni[subsample3$edu_5==1] <- 0

subsample3$status_per[subsample3$status_1==1] <- 1
subsample3$status_per[subsample3$status_2==1] <- 1
subsample3$status_per[subsample3$status_3==1] <- 0
subsample3$status_per[subsample3$status_4==1] <- 0
subsample3$status_per[subsample3$status_5==1] <- 0

subsample_var_rep<-subset(subsample3, !is.na(rep), select=c(knowledge_post, seen_logo, chances, importance_self, appreciate,  difference, discuss_online, discuss_real, protest, assignment, compliance, civic, fbtreat, fbvsnewsletter, stream, townpoplog, age_re, sex_2, uni, edu_5, status_per, status_5, friends_re, fbfriends_re, frequency_fb,  news_2, news_3, environment_act_2,  civic_re,  vote_re, scale_trust, role_2, role_3, knowledgescale, nature_re, danger,  adventure, pleasure, ability))

subsample_var_act<-subset(subsample3, !is.na(act), select=c(knowledge_post, seen_logo, chances, importance_self, appreciate,  difference, discuss_online, discuss_real, protest, assignment, compliance, civic, fbtreat, fbvsnewsletter, stream, townpoplog, age_re, sex_2, uni, status_per, status_5,  friends_re, fbfriends_re, frequency_fb, environment_act_2,  civic_re,  vote_re, scale_trust,role_2, role_3,  knowledgescale, nature_re, danger, adventure, pleasure, ability))


#Representative sample

#Discuss Online

subsample_discuss_online<-subset(subsample_var_rep,!is.na(discuss_online))

subsample_discuss_online$sdY<-sd(subsample_discuss_online$discuss_online[subsample_discuss_online$assignment==1])
subsample_discuss_online$muY<-mean(subsample_discuss_online$discuss_online[subsample_discuss_online$assignment==1])
subsample_discuss_online$Y <- subsample_discuss_online$discuss_online 

subsample_discuss_online$Yscaled <- (subsample_discuss_online$Y-subsample_discuss_online$muY)/subsample_discuss_online$sdY # define Outcome

subsample_discuss_online$Z <- subsample_discuss_online$assignment # define Treatment Assignment
subsample_discuss_online$X<- as.matrix(subsample_discuss_online[,16:39])
subsample_discuss_online$Z1 <- subsample_discuss_online$fbtreat # define Treatment Assignment
subsample_discuss_online$Z2 <- subsample_discuss_online$newsletter # define Treatment Assignment

subsample_discuss_online$blocks[subsample_discuss_online$stream=="representative"] <-0
subsample_discuss_online$blocks[subsample_discuss_online$stream=="activists"] <-1 


Z<-subsample_discuss_online$Z

blocks<-subsample_discuss_online$blocks

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N=331, m_each = c(117, 92, 122))
subsample_discuss_online$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Discuss Offline

subsample_discuss_real<-subset(subsample_var_rep,!is.na(discuss_real))

subsample_discuss_real$Y <- subsample_discuss_real$discuss_real 

subsample_discuss_real$sdY<-sd(subsample_discuss_real$discuss_real[subsample_discuss_real$assignment==1])
subsample_discuss_real$muY<-mean(subsample_discuss_real$discuss_real[subsample_discuss_real$assignment==1])

subsample_discuss_real$Yscaled <- (subsample_discuss_real$discuss_real-subsample_discuss_real$muY)/subsample_discuss_real$sdY # define Outcome


subsample_discuss_real$Z <- subsample_discuss_real$assignment # define Treatment Assignment
subsample_discuss_real$Z1 <- subsample_discuss_real$fbtreat # define Treatment Assignment
subsample_discuss_real$Z2 <- subsample_discuss_real$newsletter # define Treatment Assignment

subsample_discuss_real$X<- as.matrix(subsample_discuss_real[,16:39])

Z<-subsample_discuss_real$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N=383, m_each = c(132, 106, 145))
subsample_discuss_real$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)

subsample_discuss_real$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Others Appreciate 

subsample_appreciate<-subset(subsample_var_rep,!is.na(appreciate))

subsample_appreciate$sdY<-sd(subsample_appreciate$appreciate[subsample_appreciate$assignment==1])
subsample_appreciate$muY<-mean(subsample_appreciate$appreciate[subsample_appreciate$assignment==1])
subsample_appreciate$Y <- subsample_appreciate$appreciate 
subsample_appreciate$Yscaled <- (subsample_appreciate$Y-subsample_appreciate$muY)/subsample_appreciate$sdY # define Outcome

subsample_appreciate$Z <- subsample_appreciate$assignment # define Treatment Assignment
subsample_appreciate$Z1 <- subsample_appreciate$fbtreat # define Treatment Assignment
subsample_appreciate$Z2 <- subsample_appreciate$newsletter # define Treatment Assignment

subsample_appreciate$X<- as.matrix(subsample_appreciate[,16:39])

Z<-subsample_appreciate$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"


Design <- declare_ra(N=387, m_each = c(133,108, 146))

subsample_appreciate$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)

#Chances

subsample_chances<-subset(subsample_var_rep,!is.na(chances))

subsample_chances$sdY<-sd(subsample_chances$chances[subsample_chances$assignment==1])
subsample_chances$muY<-mean(subsample_chances$chances[subsample_chances$assignment==1])
subsample_chances$Y <- subsample_chances$chances  
subsample_chances$Yscaled <- (subsample_chances$Y-subsample_chances$muY)/subsample_chances$sdY # define Outcome


subsample_chances$Z <- subsample_chances$assignment # define Treatment Assignment
subsample_chances$Z1 <- subsample_chances$fbtreat # define Treatment Assignment
subsample_chances$Z2 <- subsample_chances$newsletter # define Treatment Assignment

subsample_chances$X<- as.matrix(subsample_chances[,16:39])

Z<-subsample_chances$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"


Design <- declare_ra(N=389, m_each = c(133,109, 147))

subsample_chances$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Differece

subsample_difference<-subset(subsample_var_rep,!is.na(difference))

subsample_difference$sdY<-sd(subsample_difference$difference[subsample_difference$assignment==1])
subsample_difference$muY<-mean(subsample_difference$difference[subsample_difference$assignment==1])
subsample_difference$Y <- subsample_difference$difference 
subsample_difference$Yscaled <- (subsample_difference$Y-subsample_difference$muY)/subsample_difference$sdY # define Outcome

table(subsample_difference$Y)

subsample_difference$Z <- subsample_difference$assignment# define Treatment Assignment
subsample_difference$Z1 <- subsample_difference$fbtreat # define Treatment Assignment
subsample_difference$Z2 <- subsample_difference$newsletter # define Treatment Assignment

subsample_difference$X<- as.matrix(subsample_difference[,16:39])

Z<-subsample_difference$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"


Design <- declare_ra(N=388, m_each = c(133,108, 147))

subsample_difference$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Importance Environment

subsample_importance<-subset(subsample_var_rep,!is.na(importance_self))

subsample_importance$sdY<-sd(subsample_importance$importance_self[subsample_importance$assignment==1])
subsample_importance$muY<-mean(subsample_importance$importance_self[subsample_importance$assignment==1])
subsample_importance$Y <- subsample_importance$importance_self 
subsample_importance$Yscaled <- (subsample_importance$Y-subsample_importance$muY)/subsample_importance$sdY # define Outcome

subsample_importance$Z <- subsample_importance$assignment # define Treatment Assignment
subsample_importance$Z1 <- subsample_importance$fbtreat # define Treatment Assignment
subsample_importance$Z2 <- subsample_importance$newsletter # define Treatment Assignment

subsample_importance$X<- as.matrix(subsample_importance[,16:39])

Z<-subsample_importance$Z


assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"


Design <- declare_ra(N=381, m_each = c(132,106, 143))

subsample_importance$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Knowledge

subsample_knowledge<-subset(subsample_var_rep,!is.na(knowledge_post))

subsample_knowledge$sdY<-sd(subsample_knowledge$knowledge_post[subsample_knowledge$assignment==1] )
subsample_knowledge$muY<-mean(subsample_knowledge$knowledge_post[subsample_knowledge$assignment==1])
subsample_knowledge$Y <- subsample_knowledge$knowledge_post  
subsample_knowledge$Yscaled <- (subsample_knowledge$Y-subsample_knowledge$muY)/subsample_knowledge$sdY # define Outcome

subsample_knowledge$Z <- subsample_knowledge$assignment# define Treatment Assignment
subsample_knowledge$Z1 <- subsample_knowledge$fbtreat # define Treatment Assignment
subsample_knowledge$Z2 <- subsample_knowledge$newsletter # define Treatment Assignment

subsample_knowledge$X<- as.matrix(subsample_knowledge[,16:39])

Z<-subsample_knowledge$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N= 388, m_each = c(133,108, 147))

subsample_knowledge$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Protest

subsample_protest<-subset(subsample_var_rep,!is.na(protest))

subsample_protest$Y <- subsample_protest$protest  # define Outcome
subsample_protest$sdY<-sd(subsample_protest$protest[subsample_protest$assignment==1] )
subsample_protest$muY<-mean(subsample_protest$protest[subsample_protest$assignment==1])
subsample_protest$Y <- subsample_protest$protest 
subsample_protest$Yscaled <- (subsample_protest$Y-subsample_protest$muY)/subsample_protest$sdY # define Outcome


subsample_protest$Z <- subsample_protest$assignment# define Treatment Assignment
subsample_protest$Z1 <- subsample_protest$fbtreat # define Treatment Assignment
subsample_protest$Z2 <- subsample_protest$newsletter # define Treatment Assignment

subsample_protest$X<- as.matrix(subsample_protest[,16:39])


Z<-subsample_protest$Z


assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N= 385, m_each = c(133,106, 146))

subsample_protest$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Discussion online

#ITT

tab_social_interaction_rep<-NA

tab_social_interaction_rep <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_social_interaction_rep$ITT<- NA
tab_social_interaction_rep$Cilower <- NA
tab_social_interaction_rep$Ciupper <- NA
tab_social_interaction_rep$outcome <- NA

colnames(tab_social_interaction_rep) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")
dim(tab_social_interaction_rep)

lm_discussonline_unadj_tab_rep <- lm(Yscaled~factor(Z), data=subsample_discuss_online)

lm_discussonline_adj_tab_rep <- lm(Yscaled~factor(Z) + X, data=subsample_discuss_online)

lm_discussonline_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_discuss_online)

lm_discussonline_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_discuss_online)

tab_social_interaction_rep[1,2] <- coef(lm_discussonline_adj)[2]
tab_social_interaction_rep[2,2] <- coef(lm_discussonline_adj)[3]

tab_social_interaction_rep[1,3]<-lm_discussonline_adj$conf.low[2]
tab_social_interaction_rep[1,4]<-lm_discussonline_adj$conf.high[2]

tab_social_interaction_rep[2,3]<-lm_discussonline_adj$conf.low[3]
tab_social_interaction_rep[2,4]<-lm_discussonline_adj$conf.high[3]


#Discussion offline

#ITT

lm_discussoffline_unadj_tab_rep <- lm(Yscaled~factor(Z), data=subsample_discuss_real)

lm_discussoffline_adj_tab_rep <- lm(Yscaled~factor(Z) + X, data=subsample_discuss_real)

lm_discussoffline_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_discuss_real)

lm_discussoffline_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_discuss_real)

tab_social_interaction_rep[3,2] <- coef(lm_discussoffline_adj)[2]
tab_social_interaction_rep[4,2] <- coef(lm_discussoffline_adj)[3]

tab_social_interaction_rep[3,3]<-lm_discussoffline_adj$conf.low[2]
tab_social_interaction_rep[3,4]<-lm_discussoffline_adj$conf.high[2]

tab_social_interaction_rep[4,3]<-lm_discussoffline_adj$conf.low[3]
tab_social_interaction_rep[4,4]<-lm_discussoffline_adj$conf.high[3]



#Appreciated

#ITT

lm_appreciate_unadj_tab_rep <- lm(Yscaled~factor(Z), data=subsample_appreciate)

lm_appreciate_adj_tab_rep <- lm(Yscaled~factor(Z) + X,data=subsample_appreciate)

lm_appreciate_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_appreciate)

lm_appreciate_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_appreciate)

tab_social_interaction_rep[5,2] <- coef(lm_appreciate_adj)[2]
tab_social_interaction_rep[6,2] <- coef(lm_appreciate_adj)[3]

tab_social_interaction_rep[5,3]<-lm_appreciate_adj$conf.low[2]
tab_social_interaction_rep[5,4]<-lm_appreciate_adj$conf.high[2]

tab_social_interaction_rep[6,3]<-lm_appreciate_adj$conf.low[3]
tab_social_interaction_rep[6,4]<-lm_appreciate_adj$conf.high[3]

tab_social_interaction_rep[1,5]<-"Discuss Online"
tab_social_interaction_rep[2,5]<-"Discuss Online"
tab_social_interaction_rep[3,5]<-"Discuss Offline"
tab_social_interaction_rep[4,5]<-"Discuss Offline"
tab_social_interaction_rep[5,5]<-"Feeling appreciated"
tab_social_interaction_rep[6,5]<-"Feeling appreciated"


#Chances

#ITT


tab_outcomes_rep<-NA

tab_outcomes_rep <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_outcomes_rep$ITT<- NA
tab_outcomes_rep$Cilower <- NA
tab_outcomes_rep$Ciupper <- NA
tab_outcomes_rep$outcome <- NA

colnames(tab_outcomes_rep) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")

lm_chances_unadj_tab_rep <- lm(Yscaled~factor(Z), data=subsample_chances)

lm_chances_adj_tab_rep <- lm(Yscaled~factor(Z) + X, data=subsample_chances)

lm_chances_unadj <- lm_robust(Yscaled~factor(Z),  data=subsample_chances)

lm_chances_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_chances)

tab_outcomes_rep[1,2] <- coef(lm_chances_adj)[2]
tab_outcomes_rep[2,2] <- coef(lm_chances_adj)[3]

tab_outcomes_rep[1,3]<-lm_chances_adj$conf.low[2]
tab_outcomes_rep[1,4]<-lm_chances_adj$conf.high[2]

tab_outcomes_rep[2,3]<-lm_chances_adj$conf.low[3]
tab_outcomes_rep[2,4]<-lm_chances_adj$conf.high[3]



#Likelihood of protest

#ITT

lm_protest_unadj_tab_rep <- lm(Yscaled~factor(Z), data=subsample_protest)

lm_protest_adj_tab_rep <- lm(Yscaled~factor(Z) + X,  data=subsample_protest)

lm_protest_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_protest)

lm_protest_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_protest)

tab_outcomes_rep[5,2] <- coef(lm_protest_adj)[2]
tab_outcomes_rep[6,2] <- coef(lm_protest_adj)[3]

tab_outcomes_rep[5,3]<-lm_protest_adj$conf.low[2]
tab_outcomes_rep[5,4]<-lm_protest_adj$conf.high[2]

tab_outcomes_rep[6,3]<-lm_protest_adj$conf.low[3]
tab_outcomes_rep[6,4]<-lm_protest_adj$conf.high[3]



#Make a difference

#ITT

lm_difference_unadj_tab_rep <- lm(Yscaled~factor(Z) , data=subsample_difference)

lm_difference_adj_tab_rep <- lm(Yscaled~factor(Z) + X  , data=subsample_difference)

lm_difference_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_difference)

lm_difference_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_difference)

tab_outcomes_rep[3,2] <- coef(lm_difference_adj)[2]
tab_outcomes_rep[4,2] <- coef(lm_difference_adj)[3]

tab_outcomes_rep[3,3]<-lm_difference_adj$conf.low[2]
tab_outcomes_rep[3,4]<-lm_difference_adj$conf.high[2]

tab_outcomes_rep[4,3]<-lm_difference_adj$conf.low[3]
tab_outcomes_rep[4,4]<-lm_difference_adj$conf.high[3]

tab_outcomes_rep[1,5]<-"Chances of success"
tab_outcomes_rep[2,5]<-"Chances of success"
tab_outcomes_rep[3,5]<-"Make a difference"
tab_outcomes_rep[4,5]<-"Make a difference"
tab_outcomes_rep[5,5]<-"Likelihood of protest"
tab_outcomes_rep[6,5]<-"Likelihood of protest"


#Environment

#ITT

tab_attitudes_rep<-NA

tab_attitudes_rep <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_attitudes_rep$ITT<- NA
tab_attitudes_rep$Cilower <- NA
tab_attitudes_rep$Ciupper <- NA
tab_attitudes_rep$outcome <- NA

colnames(tab_attitudes_rep) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")

lm_importance_unadj_tab_rep <- lm(Yscaled~factor(Z), data=subsample_importance)

lm_importance_adj_tab_rep <- lm(Yscaled~factor(Z) + X, data=subsample_importance)

lm_importance_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_importance)

lm_importance_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_importance)

tab_attitudes_rep[1,2] <- coef(lm_importance_adj)[2]
tab_attitudes_rep[2,2] <- coef(lm_importance_adj)[3]

tab_attitudes_rep[1,3]<-lm_importance_adj$conf.low[2]
tab_attitudes_rep[1,4]<-lm_importance_adj$conf.high[2]

tab_attitudes_rep[2,3]<-lm_importance_adj$conf.low[3]
tab_attitudes_rep[2,4]<-lm_importance_adj$conf.high[3]



#Knowledge

#ITT

lm_knowledge_unadj_tab_rep <- lm(Yscaled~factor(Z),  data=subsample_knowledge)

lm_knowledge_adj_tab_rep <- lm(Yscaled~factor(Z) + X,  data=subsample_knowledge)

lm_knowledge_unadj <- lm_robust(Yscaled~factor(Z),  data=subsample_knowledge)

lm_knowledge_adj <- lm_robust(Yscaled~factor(Z) + X,  data=subsample_knowledge)


tab_attitudes_rep[3,2] <- coef(lm_knowledge_adj)[2]
tab_attitudes_rep[4,2] <- coef(lm_knowledge_adj)[3]

tab_attitudes_rep[3,3]<-lm_knowledge_adj$conf.low[2]
tab_attitudes_rep[3,4]<-lm_knowledge_adj$conf.high[2]

tab_attitudes_rep[4,3]<-lm_knowledge_adj$conf.low[3]
tab_attitudes_rep[4,4]<-lm_knowledge_adj$conf.high[3]

tab_attitudes_rep[1,5]<-"perceived importance environment"
tab_attitudes_rep[2,5]<-"perceived importance environment"
tab_attitudes_rep[3,5]<-"environmental knowledge"
tab_attitudes_rep[4,5]<-"environmental knowledge"



#Activist sample

subsample_var_act$fbcomply[subsample_var_act$assignment==1] <- 0
subsample_var_act$fbcomply[subsample_var_act$assignment==2 & subsample_var_act$compliance==1] <- 1
subsample_var_act$fbcomply[subsample_var_act$assignment==2 & subsample_var_act$compliance==0] <- 0
subsample_var_act$fbcomply[subsample_var_act$assignment==3 & subsample_var_act$compliance==0] <- NA
subsample_var_act$fbcomply[subsample_var_act$assignment==3 & subsample_var_act$compliance==1] <- NA

subsample_var_act$fbtreat[subsample_var_act$assignment==1] <- 0
subsample_var_act$fbtreat[subsample_var_act$assignment==2] <- 1
subsample_var_act$fbtreat[subsample_var_act$assignment==3] <- NA

subsample_var_act$newsletter[subsample_var_act$assignment==1] <- 0
subsample_var_act$newsletter[subsample_var_act$assignment==2] <- NA
subsample_var_act$newsletter[subsample_var_act$assignment==3] <- 1

subsample_var_act$newscomply[subsample_var_act$assignment==1] <- 0
subsample_var_act$newscomply[subsample_var_act$assignment==2 & subsample_var_act$compliance==1] <- NA
subsample_var_act$newscomply[subsample_var_act$assignment==2 & subsample_var_act$compliance==0] <- NA
subsample_var_act$newscomply[subsample_var_act$assignment==3 & subsample_var_act$compliance==0] <- 0
subsample_var_act$newscomply[subsample_var_act$assignment==3 & subsample_var_act$compliance==1] <- 1



#Discuss Online

subsample_discuss_online<-subset(subsample_var_act,!is.na(discuss_online))

subsample_discuss_online$sdY<-sd(subsample_discuss_online$discuss_online[subsample_discuss_online$assignment==1])
subsample_discuss_online$muY<-mean(subsample_discuss_online$discuss_online[subsample_discuss_online$assignment==1])
subsample_discuss_online$Y <- subsample_discuss_online$discuss_online 

subsample_discuss_online$Yscaled <- (subsample_discuss_online$Y-subsample_discuss_online$muY)/subsample_discuss_online$sdY # define Outcome

subsample_discuss_online$Z <- subsample_discuss_online$assignment # define Treatment Assignment
subsample_discuss_online$X<- as.matrix(subsample_discuss_online[,16:36])


Z<-subsample_discuss_online$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N=169, m_each = c(64, 57, 48 ))
subsample_discuss_online$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Discuss Offline

subsample_discuss_real<-subset(subsample_var_act,!is.na(discuss_real))

subsample_discuss_real$Y <- subsample_discuss_real$discuss_real 

subsample_discuss_real$sdY<-sd(subsample_discuss_real$discuss_real[subsample_discuss_real$assignment==1])
subsample_discuss_real$muY<-mean(subsample_discuss_real$discuss_real[subsample_discuss_real$assignment==1])

subsample_discuss_real$Yscaled <- (subsample_discuss_real$discuss_real-subsample_discuss_real$muY)/subsample_discuss_real$sdY # define Outcome


subsample_discuss_real$Z <- subsample_discuss_real$assignment # define Treatment Assignment

subsample_discuss_real$X<- as.matrix(subsample_discuss_real[,16:36])

Z<-subsample_discuss_real$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N=176, m_each = c(66, 60, 50))
subsample_discuss_real$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)

subsample_discuss_real$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Others Appreciate 

subsample_appreciate<-subset(subsample_var_act,!is.na(appreciate))

subsample_appreciate$sdY<-sd(subsample_appreciate$appreciate[subsample_appreciate$assignment==1])
subsample_appreciate$muY<-mean(subsample_appreciate$appreciate[subsample_appreciate$assignment==1])
subsample_appreciate$Y <- subsample_appreciate$appreciate 
subsample_appreciate$Yscaled <- (subsample_appreciate$Y-subsample_appreciate$muY)/subsample_appreciate$sdY # define Outcome

subsample_appreciate$Z <- subsample_appreciate$assignment # define Treatment Assignment
subsample_appreciate$X<- as.matrix(subsample_appreciate[,16:36])

Z<-subsample_appreciate$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"


Design <- declare_ra(N= 175, m_each = c(66,60, 49))

subsample_appreciate$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Chances

subsample_chances<-subset(subsample_var_act,!is.na(chances))

table(subsample_chances$chances)

subsample_chances$sdY<-sd(subsample_chances$chances[subsample_chances$assignment==1])
subsample_chances$muY<-mean(subsample_chances$chances[subsample_chances$assignment==1])
subsample_chances$Y <- subsample_chances$chances 
subsample_chances$Yscaled <- (subsample_chances$Y-subsample_chances$muY)/subsample_chances$sdY # define Outcome

subsample_chances$Z <- subsample_chances$assignment # define Treatment Assignment
subsample_chances$X<- as.matrix(subsample_chances[,16:36])

Z<-subsample_chances$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

table(assignment)

Design <- declare_ra(N=178, m_each = c(67,61, 50))

subsample_chances$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)



#Differece

subsample_difference<-subset(subsample_var_act,!is.na(difference))

subsample_difference$sdY<-sd(subsample_difference$difference[subsample_difference$assignment==1])
subsample_difference$muY<-mean(subsample_difference$difference[subsample_difference$assignment==1])
subsample_difference$Y <- subsample_difference$difference 
subsample_difference$Yscaled <- (subsample_difference$Y-subsample_difference$muY)/subsample_difference$sdY # define Outcome

subsample_difference$Z <- subsample_difference$assignment# define Treatment Assignment
subsample_difference$X<- as.matrix(subsample_difference[,16:36])

Z<-subsample_difference$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"


Design <- declare_ra(N=177, m_each = c(67,60, 50))

subsample_difference$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Importance Environment

subsample_importance<-subset(subsample_var_act,!is.na(importance_self))

subsample_importance$sdY<-sd(subsample_importance$importance_self[subsample_importance$assignment==1])
subsample_importance$muY<-mean(subsample_importance$importance_self[subsample_importance$assignment==1])
subsample_importance$Y <- subsample_importance$importance_self
subsample_importance$Yscaled <- (subsample_importance$Y-subsample_importance$muY)/subsample_importance$sdY # define Outcome

subsample_importance$Z <- subsample_importance$assignment # define Treatment Assignment
subsample_importance$X<- as.matrix(subsample_importance[,16:36])

Z<-subsample_importance$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N=174, m_each = c(66,60, 48))

subsample_importance$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)


#Knowledge

subsample_knowledge<-subset(subsample_var_act,!is.na(knowledge_post))

subsample_knowledge$sdY<-sd(subsample_knowledge$knowledge_post[subsample_knowledge$assignment==1] )
subsample_knowledge$muY<-mean(subsample_knowledge$knowledge_post[subsample_knowledge$assignment==1])
subsample_knowledge$Y <- subsample_knowledge$knowledge_post  # define Outcome
subsample_knowledge$Yscaled <- (subsample_knowledge$Y-subsample_knowledge$muY)/subsample_knowledge$sdY # define Outcome

subsample_knowledge$Z <- subsample_knowledge$assignment# define Treatment Assignment
subsample_knowledge$X<- as.matrix(subsample_knowledge[,16:36])

Z<-subsample_knowledge$Z

assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N= 177, m_each = c(67,60, 50))

subsample_knowledge$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)



#Protest

subsample_protest<-subset(subsample_var_act,!is.na(protest))

subsample_protest$Y <- subsample_protest$protest  # define Outcome
subsample_protest$sdY<-sd(subsample_protest$protest[subsample_protest$assignment==1] )
subsample_protest$muY<-mean(subsample_protest$protest[subsample_protest$assignment==1])
subsample_protest$Yscaled <- (subsample_protest$Y-subsample_protest$muY)/subsample_protest$sdY # define Outcome

subsample_protest$Z <- subsample_protest$assignment# define Treatment Assignment
subsample_protest$X<- as.matrix(subsample_protest[,16:36])


Z<-subsample_protest$Z


assignment<-NA
assignment[Z==1]<-"T1"
assignment[Z==2]<-"T2"
assignment[Z==3]<-"T3"

Design <- declare_ra(N= 177, m_each = c(67,60, 50))

subsample_protest$prob_obs <- obtain_condition_probabilities(Design, assignment=assignment)




#Discussion online

#ITT

tab_social_interaction_act<-NA

tab_social_interaction_act <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_social_interaction_act$ITT<- NA
tab_social_interaction_act$Cilower <- NA
tab_social_interaction_act$Ciupper <- NA
tab_social_interaction_act$outcome <- NA

colnames(tab_social_interaction_act) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")

lm_discussonline_unadj_tab_act <- lm(Yscaled~factor(Z), data=subsample_discuss_online)

lm_discussonline_adj_tab_act <- lm(Yscaled~factor(Z) + X, data=subsample_discuss_online)

lm_discussonline_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_discuss_online)

lm_discussonline_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_discuss_online)

tab_social_interaction_act[1,2] <- coef(lm_discussonline_adj)[2]
tab_social_interaction_act[2,2] <- coef(lm_discussonline_adj)[3]

tab_social_interaction_act[1,3]<-lm_discussonline_adj$conf.low[2]
tab_social_interaction_act[1,4]<-lm_discussonline_adj$conf.high[2]

tab_social_interaction_act[2,3]<-lm_discussonline_adj$conf.low[3]
tab_social_interaction_act[2,4]<-lm_discussonline_adj$conf.high[3]


#Discussion offline

#ITT

table(subsample_discuss_real$Z)

lm_discussoffline_unadj_tab_act <- lm(Yscaled~factor(Z), data=subsample_discuss_real)

lm_discussoffline_adj_tab_act <- lm(Yscaled~factor(Z) + X, data=subsample_discuss_real)

lm_discussoffline_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_discuss_real)

lm_discussoffline_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_discuss_real)

tab_social_interaction_act[3,2] <- coef(lm_discussoffline_adj)[2]
tab_social_interaction_act[4,2] <- coef(lm_discussoffline_adj)[3]

tab_social_interaction_act[3,3]<-lm_discussoffline_adj$conf.low[2]
tab_social_interaction_act[3,4]<-lm_discussoffline_adj$conf.high[2]

tab_social_interaction_act[4,3]<-lm_discussoffline_adj$conf.low[3]
tab_social_interaction_act[4,4]<-lm_discussoffline_adj$conf.high[3]



#Appreciated

#ITT

lm_appreciate_unadj_tab_act <- lm(Yscaled~factor(Z),data=subsample_appreciate)

lm_appreciate_adj_tab_act <- lm(Yscaled~factor(Z) + X,data=subsample_appreciate)

lm_appreciate_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_appreciate)

lm_appreciate_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_appreciate)


tab_social_interaction_act[5,2] <- coef(lm_appreciate_adj)[2]
tab_social_interaction_act[6,2] <- coef(lm_appreciate_adj)[3]

tab_social_interaction_act[5,3]<-lm_appreciate_adj$conf.low[2]
tab_social_interaction_act[5,4]<-lm_appreciate_adj$conf.high[2]

tab_social_interaction_act[6,3]<-lm_appreciate_adj$conf.low[3]
tab_social_interaction_act[6,4]<-lm_appreciate_adj$conf.high[3]

tab_social_interaction_act[1,5]<-"Discuss Online"
tab_social_interaction_act[2,5]<-"Discuss Online"
tab_social_interaction_act[3,5]<-"Discuss Offline"
tab_social_interaction_act[4,5]<-"Discuss Offline"
tab_social_interaction_act[5,5]<-"Feeling appreciated"
tab_social_interaction_act[6,5]<-"Feeling appreciated"


#Chances

#ITT


tab_outcomes_act<-NA

tab_outcomes_act <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_outcomes_act$ITT<- NA
tab_outcomes_act$Cilower <- NA
tab_outcomes_act$Ciupper <- NA
tab_outcomes_act$outcome <- NA

colnames(tab_outcomes_act) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")
dim(tab_outcomes_act)

lm_chances_unadj_tab_act <- lm(Yscaled~factor(Z),  data=subsample_chances)

lm_chances_adj_tab_act <- lm(Yscaled~factor(Z) + X, data=subsample_chances)

lm_chances_unadj <- lm_robust(Yscaled~factor(Z),  data=subsample_chances)

lm_chances_adj <- lm_robust(Yscaled~factor(Z) + X,  data=subsample_chances)

tab_outcomes_act[1,2] <- coef(lm_chances_adj)[2]
tab_outcomes_act[2,2] <- coef(lm_chances_adj)[3]

tab_outcomes_act[1,3]<-lm_chances_adj$conf.low[2]
tab_outcomes_act[1,4]<-lm_chances_adj$conf.high[2]

tab_outcomes_act[2,3]<-lm_chances_adj$conf.low[3]
tab_outcomes_act[2,4]<-lm_chances_adj$conf.high[3]



#Likelihood of protest

#ITT

lm_protest_unadj_tab_act <- lm(Yscaled~factor(Z), data=subsample_protest)

lm_protest_adj_tab_act <- lm(Yscaled~factor(Z) + X, data=subsample_protest)

lm_protest_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_protest)

lm_protest_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_protest)

tab_outcomes_act[5,2] <- coef(lm_protest_adj)[2]
tab_outcomes_act[6,2] <- coef(lm_protest_adj)[3]

tab_outcomes_act[5,3]<-lm_protest_adj$conf.low[2]
tab_outcomes_act[5,4]<-lm_protest_adj$conf.high[2]

tab_outcomes_act[6,3]<-lm_protest_adj$conf.low[3]
tab_outcomes_act[6,4]<-lm_protest_adj$conf.high[3]


#Make a difference

#ITT

lm_difference_unadj_tab_act <- lm(Yscaled~factor(Z) ,  data=subsample_difference)

lm_difference_adj_tab_act <- lm(Yscaled~factor(Z) + X  ,  data=subsample_difference)

lm_difference_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_difference)

lm_difference_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_difference)

tab_outcomes_act[3,2] <- coef(lm_difference_adj)[2]
tab_outcomes_act[4,2] <- coef(lm_difference_adj)[3]

tab_outcomes_act[3,3]<-lm_difference_adj$conf.low[2]
tab_outcomes_act[3,4]<-lm_difference_adj$conf.high[2]

tab_outcomes_act[4,3]<-lm_difference_adj$conf.low[3]
tab_outcomes_act[4,4]<-lm_difference_adj$conf.high[3]

tab_outcomes_act[1,5]<-"Chances of success"
tab_outcomes_act[2,5]<-"Chances of success"
tab_outcomes_act[3,5]<-"Make a difference"
tab_outcomes_act[4,5]<-"Make a difference"
tab_outcomes_act[5,5]<-"Likelihood of protest"
tab_outcomes_act[6,5]<-"Likelihood of protest"


#Environment

#ITT

tab_attitudes_act<-NA

tab_attitudes_act <- data.frame(c(rep("Facebook",1), rep("Email",1), rep("Facebook",1), rep("Email",1)))
tab_attitudes_act$ITT<- NA
tab_attitudes_act$Cilower <- NA
tab_attitudes_act$Ciupper <- NA
tab_attitudes_act$outcome <- NA

colnames(tab_attitudes_act) <- c("Treatment", "ITT","Ci_lower", "Ci_upper", "Outcome")
dim(tab_attitudes_act)

lm_importance_unadj_tab_act <- lm(Yscaled~factor(Z), data=subsample_importance)

lm_importance_adj_tab_act <- lm(Yscaled~factor(Z) + X,  data=subsample_importance)


lm_importance_unadj <- lm_robust(Yscaled~factor(Z), data=subsample_importance)

lm_importance_adj <- lm_robust(Yscaled~factor(Z) + X, data=subsample_importance)

tab_attitudes_act[1,2] <- coef(lm_importance_adj)[2]
tab_attitudes_act[2,2] <- coef(lm_importance_adj)[3]

tab_attitudes_act[1,3]<-lm_importance_adj$conf.low[2]
tab_attitudes_act[1,4]<-lm_importance_adj$conf.high[2]

tab_attitudes_act[2,3]<-lm_importance_adj$conf.low[3]
tab_attitudes_act[2,4]<-lm_importance_adj$conf.high[3]


#Knowledge

#ITT

lm_knowledge_unadj_tab_act <- lm(Yscaled~factor(Z),data=subsample_knowledge)

lm_knowledge_adj_tab_act <- lm(Yscaled~factor(Z) + X,  data=subsample_knowledge)


lm_knowledge_unadj <- lm_robust(Yscaled~factor(Z),data=subsample_knowledge)

lm_knowledge_adj <- lm_robust(Yscaled~factor(Z) + X,  data=subsample_knowledge)



tab_attitudes_act[3,2] <- coef(lm_knowledge_adj)[2]
tab_attitudes_act[4,2] <- coef(lm_knowledge_adj)[3]

tab_attitudes_act[3,3]<-lm_knowledge_adj$conf.low[2]
tab_attitudes_act[3,4]<-lm_knowledge_adj$conf.high[2]

tab_attitudes_act[4,3]<-lm_knowledge_adj$conf.low[3]
tab_attitudes_act[4,4]<-lm_knowledge_adj$conf.high[3]

tab_attitudes_act[1,5]<-"perceived importance environment"
tab_attitudes_act[2,5]<-"perceived importance environment"
tab_attitudes_act[3,5]<-"environmental knowledge"
tab_attitudes_act[4,5]<-"environmental knowledge"


#Figure 8

pdf(paste("results_social_interaction_rep.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_social_interaction_rep,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on social interaction") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("ITT (in sd)")
dev.off() 

pdf(paste("results_outcomes_rep.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_outcomes_rep,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on perceptions of activism") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("ITT (in sd)")
dev.off() 

pdf(paste("results_attitudes_rep.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_attitudes_rep,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on environmental attitudes/knowledge") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("ITT (in sd)")
dev.off() 

pdf(paste("results_social_interaction_act.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_social_interaction_act,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on social interaction") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("ITT (in sd)")
dev.off() 

pdf(paste("results_outcomes_act.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_outcomes_act,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on perceptions of activism") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("ITT (in sd)")
dev.off() 

pdf(paste("results_attitudes_act.pdf"),w=8,h=5)
graphdb3 <- ggplot(tab_attitudes_act,aes(x = Outcome, y = ITT,ymin = Ci_lower, ymax = Ci_upper, color=Treatment))
graphdb3 + scale_colour_manual(values=c("grey50", "black")) + geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
  geom_linerange(position=position_dodge(width=0.5), size =0.5) +
  geom_hline(yintercept=0, colour="grey", linetype = "dashed", size = 0.3) +
  theme_bw() + ggtitle("ITT on environmental attitudes/knowledge") + coord_cartesian(ylim = c(-0.75, 0.75)) +  ylab("ITT (in sd)")
dev.off() 

sink("tables.txt")

print(table1)

print(table2)

sink()

print(sI, RNG = TRUE, locale = FALSE)